/*
 * Student Name: Philippe Gambling
 * Course: CSCI 5733
 * Assignment: Homework 1
 * Created: Jun 17, 2009
 * Modified: Jun 17, 2009
 * 
 * Class Name: NewsItem
 * Description: Represents an individual news item retrieved from
 *              a news source.
 */

package csci5733.hw1;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author Philippe Gambling
 */
public class NewsItem implements Comparable {
    private String source;
    private Date time; // Used for comparisons
    private String timeStr; // Used to keep original source's time format
    private String url;
    private String title;
    private static DateFormat df1 = new SimpleDateFormat("MMM d yyyy h:mma");
    private static DateFormat df2 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");

    public NewsItem(String source) {
        this.source = source;
        this.time = new Date();
        this.timeStr = new String();
        this.url = new String();
        this.title = new String();
    }

    /**
     * @return the source
     */
    public String getSource() {
        return source;
    }

    /**
     * @param source the source to set
     */
    public void setSource(String source) {
        this.source = source;
    }

    /**
     * @return the time
     */
    public Date getTime() {
        return time;
    }

     /**
     * @return the time as string
     */
    public String getTimeStr() {
        return timeStr;
    }


    /**
     * @param time the time to set
     */
    public void setTime(String date) throws ParseException {
        timeStr = date;
        try {
            this.time = NewsItem.df1.parse(date);
        } catch (ParseException ex) {
            // Try the second time format
            this.time = NewsItem.df2.parse(date);
        }

    }

    /**
     * @return the url
     */
    public String getUrl() {
        return url;
    }

    /**
     * @param url the url to set
     */
    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public int compareTo(Object o) {
        if (!(o instanceof NewsItem))
            throw new ClassCastException("A NewsItem object expected.");
        int result = this.time.compareTo(((NewsItem)o).getTime());
        if(result == 0) {
            result = this.title.compareTo(((NewsItem)o).getTitle());
        }
        if(result == 0) {
            result = this.title.compareTo(((NewsItem)o).getUrl());
        }
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final NewsItem other = (NewsItem) obj;
        if ((this.source == null) ? (other.source != null) : !this.source.equals(other.source)) {
            return false;
        }
        if (this.time != other.time && (this.time == null || !this.time.equals(other.time))) {
            return false;
        }
        if ((this.timeStr == null) ? (other.timeStr != null) : !this.timeStr.equals(other.timeStr)) {
            return false;
        }
        if ((this.url == null) ? (other.url != null) : !this.url.equals(other.url)) {
            return false;
        }
        if ((this.title == null) ? (other.title != null) : !this.title.equals(other.title)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 79 * hash + (this.source != null ? this.source.hashCode() : 0);
        hash = 79 * hash + (this.time != null ? this.time.hashCode() : 0);
        hash = 79 * hash + (this.timeStr != null ? this.timeStr.hashCode() : 0);
        hash = 79 * hash + (this.url != null ? this.url.hashCode() : 0);
        hash = 79 * hash + (this.title != null ? this.title.hashCode() : 0);
        return hash;
    }


}
